#!/usr/bin/env python

from banner_class import Banner


# {{{ GROUPS MANAGEMENT
def group_add ( liwe, name, enabled, expiration = None ):
	"""
	:INPUT:

		- name:			SAFE_DB_STRING
		- enabled:		SAFE_DB_BOOL
		- expiration:		SAFE_DB_DATETIME

	:PERMS:
		- create:		With this permission you can create/modify
	"""

	b = Banner ( liwe )
	id_group = b.group_add ( name, enabled, expiration )

	return { "id_group": id_group }


def group_change ( liwe, id_group, name, enabled, expiration = None ):
	"""
	:INPUT:

		- id_group:		SAFE_DB_INTEGER
		- name:			SAFE_DB_STRING
		- enabled:		SAFE_DB_BOOL
		- expiration:		SAFE_DB_DATETIME

	:PERMS:
		- create:		With this permission you can create/modify
	"""

	b = Banner ( liwe )
	b.group_change ( id_group, name, enabled, expiration )


def group_del ( liwe, ids ):
	"""
	:INPUT:

		- ids:			SAFE_STRING

	:PERMS:
		- create:		With this permission you can create/modify
	"""

	b = Banner ( liwe )
	b.group_del ( ids )


def group_list ( liwe, mode = "ds", _X_LINES = 10, _X_START_POINT = 0, _X_PAGE = 0 ):
	"""
	:INPUT:
		- _X_LINES:		SAFE_NUMBER
		- _X_START_POINT:	SAFE_NUMBER
		- _X_PAGE:		SAFE_NUMBER
		- mode:			SAFE_STRING


	:PERMS:
		- create:		With this permission you can create/modify
	"""

	if _X_START_POINT:
		start_point = _X_START_POINT
	else:
		start_point = _X_PAGE * _X_LINES

	if mode == 'ds':
		limit = _X_LINES
	else:
		limit = 9999999

	b = Banner ( liwe )
	res = b.group_list ( start_point, limit )

	if mode == 'ds':
		count = 0
		for v in res [ 'rows' ]:
			v [ "_pos" ] = start_point + count
			res [ "row%d" % count ] = v

			count += 1

		res [ 'rows' ] = res [ 'tot_rows' ]

		res [ 'from_row' ] = start_point
		res [ 'to_row' ] = start_point + count
		res [ 'lines' ] = _X_LINES
	else:
		res = { "groups": res [ "rows" ] }

	return res


def group_get ( liwe, id_group ):
	"""
	:INPUT:

		- id_group:		SAFE_DB_INTEGER

	:PERMS:
		- create:		With this permission you can create/modify
	"""

	b = Banner ( liwe )
	res = b.group_get ( id_group )

	return { "group": res }
# }}}

# {{{ CONTAINERS MANAGEMENT
def container_add ( liwe, name, issue_rate, width, height ):
	"""
	:INPUT:

		- name:			SAFE_DB_STRING
		- issue_rate:		SAFE_DB_INTEGER
		- width:		SAFE_DB_INTEGER
		- height:		SAFE_DB_INTEGER

	:PERMS:
		- create:		With this permission you can create/modify
	"""

	b = Banner ( liwe )
	id_cont = b.container_add ( name, issue_rate, width, height )

	return { "id_cont": id_cont }


def container_change ( liwe, id_cont, name, issue_rate, width, height ):
	"""
	:INPUT:

		- id_cont:		SAFE_DB_INTEGER
		- name:			SAFE_DB_STRING
		- issue_rate:		SAFE_DB_INTEGER
		- width:		SAFE_DB_INTEGER
		- height:		SAFE_DB_INTEGER

	:PERMS:
		- create:		With this permission you can create/modify
	"""

	b = Banner ( liwe )
	b.container_change ( id_cont, name, issue_rate, width, height )


def container_del ( liwe, ids ):
	"""
	:INPUT:

		- ids:			SAFE_STRING

	:PERMS:
		- create:		With this permission you can create/modify
	"""

	b = Banner ( liwe )
	b.container_del ( ids )


def container_list ( liwe ):
	"""
	:INPUT:


	:PERMS:
		- create:		With this permission you can create/modify
	"""

	b = Banner ( liwe )
	res = b.container_list ()

	return { "containers": res }

def container_list2 ( liwe, mode = "ds", _X_LINES = 10, _X_START_POINT = 0, _X_PAGE = 0 ):
	"""
	:INPUT:
		- _X_LINES:		SAFE_NUMBER
		- _X_START_POINT:	SAFE_NUMBER
		- _X_PAGE:		SAFE_NUMBER
		- mode:			SAFE_STRING


	:PERMS:
		- create:		With this permission you can create/modify
	"""

	if _X_START_POINT:
		start_point = _X_START_POINT
	else:
		start_point = _X_PAGE * _X_LINES

	if mode == 'ds':
		limit = _X_LINES
	else:
		limit = 9999999

	b = Banner ( liwe )
	res = b.container_list2 ( start_point, limit )

	if mode == 'ds':
		count = 0
		for v in res [ 'rows' ]:
			v [ "_pos" ] = start_point + count
			res [ "row%d" % count ] = v

			count += 1

		res [ 'rows' ] = res [ 'tot_rows' ]

		res [ 'from_row' ] = start_point
		res [ 'to_row' ] = start_point + count
		res [ 'lines' ] = _X_LINES
	else:
		res = { "containers": res [ "rows" ] }

	return res


def container_get ( liwe, id_cont ):
	"""
	:INPUT:

		- id_cont:		SAFE_DB_INTEGER

	:PERMS:
		- create:		With this permission you can create/modify
	"""

	b = Banner ( liwe )
	res = b.container_get ( id_cont )

	return { "container": res }
# }}}

# {{{ BANNER MANAGEMENT
def banner_add ( liwe, id_container, popup, issue_rate, id_group = 0, url_click = "", view_time = 0 ):
	"""
	:INPUT:

		- id_container:			SAFE_DB_INTEGER
		- id_group:			SAFE_DB_INTEGER
		- url_click:			SAFE_DB_STRING
		- popup:			SAFE_DB_BOOL
		- issue_rate:			SAFE_DB_INTEGER
		- view_time:			SAFE_DB_INTEGER

	:PERMS:
		- create:		With this permission you can create/modify
	"""

	b = Banner ( liwe )
	id_banner = b.banner_add ( id_container, id_group, url_click, popup, issue_rate, view_time )

	return { "id_banner": id_banner }


def banner_change ( liwe, id_banner, id_container, popup, issue_rate, id_group = 0, url_click = "", view_time = 0 ):
	"""
	:INPUT:
		- id_banner:			SAFE_DB_INTEGER
		- id_container:			SAFE_DB_INTEGER
		- id_group:			SAFE_DB_INTEGER
		- url_click:			SAFE_DB_STRING
		- popup:			SAFE_DB_BOOL
		- issue_rate:			SAFE_DB_INTEGER
		- view_time:			SAFE_DB_INTEGER

	:PERMS:
		- create:		With this permission you can create/modify
	"""

	b = Banner ( liwe )
	b.banner_change ( id_banner, id_container, id_group, url_click, popup, issue_rate, view_time )


def banner_del ( liwe, ids ):
	"""
	:INPUT:
		- ids:			SAFE_STRING

	:PERMS:
		- create:		With this permission you can create/modify
	"""

	b = Banner ( liwe )
	b.banner_del ( ids )

def banner_list ( liwe, id_group = None, id_container = None ):
	"""
	:INPUT:
		- id_container:			SAFE_DB_INTEGER
		- id_group:			SAFE_DB_INTEGER

	:PERMS:
		- create:		With this permission you can create/modify
	"""

	b = Banner ( liwe )
	res = b.banner_list ( id_group, id_container )

	return { "banners": res }

def banner_list2 ( liwe, mode = "ds", _X_LINES = 10, _X_START_POINT = 0, _X_PAGE = 0 ):
	"""
	:INPUT:
		- _X_LINES:		SAFE_NUMBER
		- _X_START_POINT:	SAFE_NUMBER
		- _X_PAGE:		SAFE_NUMBER
		- mode:			SAFE_STRING


	:PERMS:
		- create:		With this permission you can create/modify
	"""

	if _X_START_POINT:
		start_point = _X_START_POINT
	else:
		start_point = _X_PAGE * _X_LINES

	if mode == 'ds':
		limit = _X_LINES
	else:
		limit = 9999999

	b = Banner ( liwe )
	res = b.banner_list2 ( start_point, limit )

	if mode == 'ds':
		count = 0
		for v in res [ 'rows' ]:
			v [ "_pos" ] = start_point + count
			res [ "row%d" % count ] = v

			count += 1

		res [ 'rows' ] = res [ 'tot_rows' ]

		res [ 'from_row' ] = start_point
		res [ 'to_row' ] = start_point + count
		res [ 'lines' ] = _X_LINES
	else:
		res = { "banners": res [ "rows" ] }

	return res

def banner_get ( liwe, id_banner ):
	"""
	:INPUT:
		- id_banner:			SAFE_DB_INTEGER

	:PERMS:
		- create:		With this permission you can create/modify
	"""

	b = Banner ( liwe )
	res = b.banner_get ( id_banner )

	return { "banner": res }


def banner_set_media ( liwe, id_banner, id_media ):
	"""
	:INPUT:
		- id_banner:			SAFE_DB_INTEGER
		- id_group:			SAFE_DB_INTEGER

	:PERMS:
		- create:		With this permission you can create/modify
	"""

	b = Banner ( liwe )
	b.banner_set_media ( id_banner, id_media )
# }}}

# {{{ STATS
'''
def get_counters ( liwe ):
	"""
	:PERMS:
		- create:		With this permission you can create/modify
	"""

	b = Banner ( liwe )
	res = b.get_counters ()

	return { "counters": res }
'''

def counters_list ( liwe, mode = "ds", _X_LINES = 10, _X_START_POINT = 0, _X_PAGE = 0 ):
	"""
	:INPUT:
		- _X_LINES:		SAFE_NUMBER
		- _X_START_POINT:	SAFE_NUMBER
		- _X_PAGE:		SAFE_NUMBER
		- mode:			SAFE_STRING


	:PERMS:
		- create:		With this permission you can create/modify
	"""

	if _X_START_POINT:
		start_point = _X_START_POINT
	else:
		start_point = _X_PAGE * _X_LINES

	if mode == 'ds':
		limit = _X_LINES
	else:
		limit = 9999999

	b = Banner ( liwe )
	res = b.counters_list ( start_point = start_point, limit = limit )

	if mode == 'ds':
		count = 0
		for v in res [ 'rows' ]:
			v [ "_pos" ] = start_point + count
			#v [ "count_" + v [ "_str_kind" ] ] = v [ "count" ]
			res [ "row%d" % count ] = v

			count += 1

		res [ 'rows' ] = res [ 'tot_rows' ]

		res [ 'from_row' ] = start_point
		res [ 'to_row' ] = start_point + count
		res [ 'lines' ] = _X_LINES
	else:
		res = { "counters": res [ "rows" ] }

	return res


def get_stats ( liwe, start = '', end = '' ):
	"""
	:PERMS:
		- create:		With this permission you can create/modify
	"""

	b = Banner ( liwe )
	res = b.get_stats ( start, end )

	import csv
	import StringIO

	out = StringIO.StringIO ()
	dw = csv.DictWriter ( out, ( "created", "id_obj", "container", "grp", "url_click", "event" ), extrasaction = 'ignore' )

	dw.writerows ( res )

	out = out.getvalue ()

	return { "stats": out }
# }}}
